package com.dyw.spzx.manager.service.lmpl;


import com.dyw.spzx.manager.mapper.SysRoleMenuMapper;
import com.dyw.spzx.manager.service.SysMenuService;
import com.dyw.spzx.manager.service.SysRoleMenuService;
import com.dyw.spzx.model.dto.system.AssginMenuDto;
import com.dyw.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    // 回显当前角色拥有的菜单
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {

        // 查询所有菜单数据
        List<SysMenu> sysMenuList = sysMenuService.findNodes();

        // 查询当前角色拥有的菜单
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);

        Map<String,Object> result = new HashMap<>();
        result.put("sysMenuList",sysMenuList);
        result.put("roleMenuIds",roleMenuIds);

        return result;
    }

    // 保存角色分配的菜单
    @Transactional
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        // 根据角色ID 删除角色拥有的菜单
        sysRoleMenuMapper.removeByRoleId(assginMenuDto.getRoleId());
        // 获取菜单ID
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (menuIdList != null && menuIdList.size() > 0) {
            sysRoleMenuMapper.doAssign(assginMenuDto);
        }


    }
}
